﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Cookie.aspx.cs" Inherits="Js_Study.Cookie.Cookie" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
    </form>
</body>
</html>
<script type="text/javascript">
    var cookies = function () {
        this.addCookie = function (name, value, overwrite) {
            var bl = this.checkExist(name);
            if (bl == true) {
                if (overwrite == true || overwrite == undefined) {
                    cookieStr = name + "=" + value;
                    document.cookie = cookieStr;
                } else {
                    var val = this.getCookie(name);
                    val = val + "," + value;
                    cookieStr = name + "=" + val;
                    document.cookie = cookieStr;
                }
            } else {
                cookieStr = name + "=" + value;
                document.cookie = cookieStr;
            }
        }

        this.checkExist = function (name) {
            var bl = false;

            var cookieStr = document.cookie;
            var arrCookie = cookieStr.split(';');
            for (var i = 0; i < arrCookie.length; i++) {
                var arr = arrCookie[i].split("=");
                if (arr[0].trim() == name) {
                    bl = true;
                    break;
                }
            }

            return bl;
        }

        this.getCookie = function (name) {
            var val = "";
            var cookieStr = document.cookie;
            var arrCookie = cookieStr.split(';');
            for (var i = 0; i < arrCookie.length; i++) {
                var arr = arrCookie[i].split("=");
                if (arr[0].trim() == name) {
                    val = arr[1];
                    break;
                }
            }
            return val;
        }

        this.delCookie = function (name) {

        }
    }

    var c = new cookies();
    c.addCookie("name", "peng");
    c.addCookie("age", "12",false);
    c.addCookie("address", "bj",false);

    c.getCookie("name");
</script>
